﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.InventoryUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import Display.MarketUI.*;
    import org.aswing.event.*;
    import Display.aswingui.*;

    public class InventoryCell extends MarketCell {

        public static const COUNT_PANE_SIZE:int = 20;
        public static const OVERLAP_AMOUNT:int = 28;
        public static const BOTTOM_GAP:int = 1;
        public static const BOTTOM_GAP_WISHLIST:int = 22;
        public static const BOTTOM_GAP_WISHLIST_USEBUTTON:int = 51;
        public static const BOTTOM_GAP_GIFT:int = 22;
        public static const CELL_BORDER_GAP:int = 6;

        protected var m_countPane:JPanel;
        protected var m_countLabel:JLabel;
        protected var m_useButton:JButton;
        protected var m_giftButton:JButton;
        protected var m_overlapPane:JPanel;
        protected var m_initialized:Boolean = false;
        protected var m_bottomPane:JPanel;
        protected var m_removePane:JPanel;
        protected var m_removeButton:JButton;

        public function InventoryCell(_arg1:LayoutManager=null){
            super(_arg1);
            this.setAssetDict(InventoryView.assetDict);
            Global.stage.addEventListener(DataItemEvent.WISHLIST_CHANGED, this.onWishlistChanged, false, 1, true);
        }
        override protected function buildCell():void{
            var _local1:JPanel;
            var _local2:JPanel;
            m_alignmentContainer = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local1 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            if (m_item.unlock == "level"){
                if (Global.player.level < m_item.requiredLevel){
                    m_itemLocked = true;
                } else {
                    m_itemLocked = false;
                };
            } else {
                if (m_item.unlock == "neighbors"){
                };
            };
            m_content = (new (m_assetDict["inventory_card"])() as DisplayObject);
            this.setPreferredWidth((m_content.width + (CELL_BORDER_GAP * 2)));
            this.setMinimumWidth((m_content.width + (CELL_BORDER_GAP * 2)));
            this.setMaximumWidth((m_content.width + (CELL_BORDER_GAP * 2)));
            m_bgDec = new MarginBackground(m_content);
            m_alignmentContainer.setBackgroundDecorator(m_bgDec);
            m_alignmentContainer.setPreferredSize(new IntDimension(m_content.width, m_content.height));
            m_alignmentContainer.setMinimumSize(new IntDimension(m_content.width, m_content.height));
            m_alignmentContainer.setMaximumSize(new IntDimension(m_content.width, m_content.height));
            this.m_overlapPane = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.BOTTOM, -(OVERLAP_AMOUNT));
            m_itemIconPane = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            m_alignmentContainer.append(m_itemIconPane);
            ASwingHelper.prepare(m_alignmentContainer);
            _local1.append(m_alignmentContainer);
            if (!m_item.persistent){
                _local2 = this.makeRemoveButtonPanel();
                if (_local2){
                    _local1.append(ASwingHelper.horizontalStrut(-(_local2.width)));
                    _local1.append(_local2);
                };
            };
            this.m_overlapPane.append(_local1);
            this.m_bottomPane = this.makeBottomPane();
            this.m_overlapPane.append(this.m_bottomPane);
            ASwingHelper.prepare(this.m_overlapPane);
            this.append(this.m_overlapPane);
            this.append(this.makeNameAndSetPane());
            ASwingHelper.prepare(this);
            this.m_initialized = true;
        }
        protected function makeRemoveButtonPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(AsWingConstants.TOP);
            var _local2:DisplayObject = (new (m_assetDict["btn_close_small_up"])() as DisplayObject);
            var _local3:DisplayObject = (new (m_assetDict["btn_close_small_over"])() as DisplayObject);
            var _local4:DisplayObject = (new (m_assetDict["btn_close_small_down"])() as DisplayObject);
            this.m_removePane = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.RIGHT);
            this.m_removeButton = new JButton();
            this.m_removeButton.wrapSimpleButton(new SimpleButton(_local2, _local3, _local4, _local2));
            this.m_removeButton.setFont(ASwingHelper.getBoldFont(12));
            this.m_removeButton.setForeground(new ASColor(0xFFFFFF));
            this.m_removeButton.filters = [new GlowFilter(0)];
            this.m_removeButton.addEventListener(MouseEvent.MOUSE_UP, this.onRemove, false, 0, true);
            this.m_removePane.append(this.m_removeButton);
            ASwingHelper.prepare(this.m_removePane);
            _local1.append(this.m_removePane);
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        protected function makeNameAndSetPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:JTextField = ASwingHelper.makeTextField("", EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12, 2645358);
            _local2.setText(m_item.localizedName);
            _local1.append(_local2);
            var _local3:Set = Global.gameSettings.getItemByName(m_item.name).getSet();
            if (_local3){
                m_setIcon = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
                _local1.append(m_setIcon);
            };
            return (_local1);
        }
        protected function makeBottomPane():JPanel{
            var _local1:JPanel = new JPanel(new BorderLayout());
            this.m_useButton = this.makeUseButton();
            if (this.m_useButton){
                _local1.append(this.m_useButton, BorderLayout.WEST);
            };
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            _local2.appendAll(ASwingHelper.verticalStrut(10), this.makeCountPane());
            ASwingHelper.setEasyBorder(_local2, 0, 0, 0, 5);
            _local1.append(_local2, BorderLayout.EAST);
            ASwingHelper.setEasyBorder(_local1, 0, 5, 5, 5);
            return (_local1);
        }
        protected function makeGiftButton():JButton{
            this.m_giftButton = new JButton("");
            this.m_giftButton.setForeground(new ASColor(0xFFFFFF));
            var _local1:DisplayObject = (new (m_assetDict["gift_up"])() as DisplayObject);
            var _local2:DisplayObject = (new (m_assetDict["gift_down"])() as DisplayObject);
            var _local3:DisplayObject = (new (m_assetDict["gift_over"])() as DisplayObject);
            this.m_giftButton.wrapSimpleButton(new SimpleButton(_local1, _local3, _local2, _local1));
            this.m_giftButton.addActionListener(this.giftItem, 0, true);
            return (this.m_giftButton);
        }
        protected function makeUseButton():JButton{
            var _local2:DisplayObject;
            var _local3:DisplayObject;
            var _local4:DisplayObject;
            var _local1:JButton = new JButton("");
            _local1.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, 16));
            _local1.setForeground(new ASColor(0xFFFFFF));
            if (this.allowUseButton()){
                this.m_overlapPane.addEventListener(MouseEvent.CLICK, this.useItem, false, 0, true);
                return (null);
            };
            if (this.allowWishlistButton()){
                _local2 = (new (m_assetDict["add_to_wishlist"])() as DisplayObject);
            } else {
                return (null);
            };
            _local1.wrapSimpleButton(new SimpleButton(_local2, _local2, _local2, _local2));
            _local1.addActionListener(this.wishItem, 0, true);
            return (_local1);
        }
        protected function makeCountPane():JPanel{
            var _local2:DisplayObject;
            var _local3:String;
            this.m_countPane = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local4:uint = 0xFFFFFF;
            if (m_item.inventoryLimit != -1){
                _local3 = ((String(this.getCount()) + "/") + String(m_item.inventoryLimit));
                if (this.getCount() == m_item.inventoryLimit){
                    _local2 = (new (m_assetDict["countMaxBG"])() as DisplayObject);
                } else {
                    _local2 = (new (m_assetDict["countBG"])() as DisplayObject);
                };
            } else {
                _local2 = (new (m_assetDict["countBG"])() as DisplayObject);
                _local3 = String(this.getCount());
            };
            this.m_countPane.setBackgroundDecorator(new AssetBackground(_local2));
            this.m_countLabel = ASwingHelper.makeLabel(_local3, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 10, _local4);
            if (m_item.inventoryLimit == -1){
                ASwingHelper.setEasyBorder(this.m_countLabel, 0, 3, 0, 3);
            };
            _local1.append(this.m_countLabel);
            this.m_countPane.appendAll(_local1);
            ASwingHelper.prepare(this.m_countPane);
            return (this.m_countPane);
        }
        protected function getCount():int{
            return (Global.player.inventory.getItemCount(m_item));
        }
        protected function allowUseButton():Boolean{
            var _local1:Boolean = Boolean(((m_item.placeable) || (m_item.consumable)));
            return (_local1);
        }
        protected function allowWishlistButton():Boolean{
            return (Global.player.canAddToWishlist(m_item.name));
        }
        private function useItem(_arg1:MouseEvent):void{
            if ((_arg1.target is SimpleButton)){
                return;
            };
            dispatchEvent(new DataItemEvent(DataItemEvent.MARKET_BUY, this.m_item, null, true));
            this.invalidateData();
        }
        private function wishItem(_arg1:AWEvent):void{
            if (Global.player.canAddToWishlist(m_item.name)){
                GameTransactionManager.addTransaction(new TAddToWishlist(m_item.name));
                dispatchEvent(new DataItemEvent(DataItemEvent.WISHLIST_CHANGED, this.m_item, null, true));
            };
        }
        private function closeInventory():void{
            var _local2:DisplayObject;
            var _local3:Boolean;
            var _local4:InventoryView;
            var _local1:int = Global.ui.numChildren;
            while (_local1--) {
                _local2 = Global.ui.getChildAt(_local1);
                _local3 = Boolean((_local2 is InventoryView));
                if (_local3){
                    _local4 = (_local2 as InventoryView);
                    _local4.close();
                    _local4.allowClose();
                    break;
                };
            };
        }
        private function giftItem(_arg1:AWEvent):void{
        }
        override protected function initializeCell():void{
            if (!m_itemIcon){
                return;
            };
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:AssetPane = new AssetPane(m_itemIcon);
            if (this.getCount() == 0){
                _local2.alpha = 0.5;
            } else {
                _local2.alpha = 1;
            };
            var _local3:Number = ((m_content.width - m_itemIcon.width) / 2);
            _local1.append(ASwingHelper.horizontalStrut(_local3));
            _local1.append(_local2);
            ASwingHelper.prepare(_local1);
            m_itemIconPane.append(_local1);
        }
        public function invalidateData():void{
            var _local1:String;
            if (Global.ui.inventoryView){
                Global.ui.inventoryView.refreshInventoryLimit();
            };
            if (this.m_initialized){
                if (m_item.inventoryLimit != -1){
                    _local1 = ((String(this.getCount()) + "/") + String(m_item.inventoryLimit));
                } else {
                    _local1 = String(this.getCount());
                };
                this.m_countLabel.setText(_local1);
            };
        }
        override public function performUpdate(_arg1:Boolean=false):void{
            this.removeAll();
            this.buildCell();
            this.initializeCell();
        }
        protected function onWishlistChanged(_arg1:DataItemEvent):void{
            if (_arg1.item.name == m_item.name){
                this.performUpdate();
            };
        }
        protected function onRemove(_arg1:MouseEvent):void{
            _arg1.stopPropagation();
            _arg1.stopImmediatePropagation();
            var _local2:RemoveItemDialog = new RemoveItemDialog(m_item.name, this.onRemoveConfirm);
            UI.displayPopup(_local2, false, "", true);
        }
        protected function onRemoveConfirm(_arg1:GenericPopupEvent):void{
            if (!(_arg1 is RemoveItemEvent)){
                return;
            };
            var _local2:String = (_arg1 as RemoveItemEvent).itemName;
            var _local3:int = (_arg1 as RemoveItemEvent).itemCount;
            Global.player.inventory.removeItems(_local2, _local3, false);
            TransactionManager.addTransaction(new TRemoveFromInventory(_local2, _local3));
            if (Global.ui.inventoryView){
                Global.ui.inventoryView.refreshInventory();
            };
        }

    }
}//package Display.InventoryUI 
